﻿using Inovout.Descriptors;
using Inovout.Models;
using NHibernate.Proxy;
using System;

namespace Inovout.Settings
{
    public partial class ObjectSettings
    {
        public ObjectSettings(object scope, Type kindType)
        {
            Scope = scope;
            KindType = kindType;
        }
        private object scope;
        public virtual object Scope
        {
            get
            {
                if (scope == null)
                {
                    if (scopeType.IsEnum)
                    {
                        EnumDescriptor enumDescriptor = EnumDescriptor.Get(scopeType);
                        scope = enumDescriptor.GetEnum(scopeIdentity);
                    }
                    else
                    {
                        throw new NotSupportedException("暂不支持直接通常ID获取DataObject对象实例。");
                    }
                }
                return scope;
            }
            set
            {
                scope = value;

                scopeType = TypeHelper.GetType(scope);

                //因为在关联对象时，ID并未生成。
                if (scope is DataObject)
                {
                    scopeIdentity = ((DataObject)scope).Id.ToString();
                }
                else
                {
                    scopeIdentity = scope.ToString();
                }
                // Key = Key ?? scopeType.Name + "_" + ScopeIdentity;
            }
        }
    }
}
